package com.xdxc.service.strategy.gradingstrategy;


import com.xdxc.annotation.GradingStrategyType;
import com.xdxc.emuns.QuestionType;
import com.xdxc.model.enums.AutoGradeQuestionType;
import com.xdxc.model.dto.CorrectAnswerDTO;
import com.xdxc.model.entity.GradingResult;
import com.xdxc.model.entity.TempUserAnswer;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

@Component
@GradingStrategyType(AutoGradeQuestionType.JUDGMENT)
public class JudgmentGradingStrategy   implements GradingStrategy{
    @Override
    public GradingResult grade(TempUserAnswer tempUserAnswer, CorrectAnswerDTO correctAnswerDTO) {
        // 1. 验证答案格式

            // 1. 验证答案格式
        Set<Long> selectedIds =new HashSet<>(tempUserAnswer.getAnswerIds());
//        // 2. 验证选项有效性
            BigDecimal score = null;
            boolean isCorrect = false;
////         3. 获取正确答案
                Set<String> correctAnswerSet = Optional.ofNullable(correctAnswerDTO.getOptionIds())
                        .orElse(Collections.emptyList())
                        .stream()
                        .map(String::valueOf)  // 确保所有 ID 是 String 类型
                        .collect(Collectors.toSet());
                isCorrect=correctAnswerSet.equals(selectedIds);
                score= isCorrect ? BigDecimal.valueOf(correctAnswerDTO.getScore()) : BigDecimal.ZERO;




        GradingResult gradingResult1 = new GradingResult();
        gradingResult1.setAnswerId(tempUserAnswer.getId());
        gradingResult1.setIsCorrect(isCorrect);
        gradingResult1.setQuestionType(QuestionType.getCodeByDesc(String.valueOf(tempUserAnswer.getAnswerType())));
        gradingResult1.setUserScore(score);

        gradingResult1.setPaperId(tempUserAnswer.getPaperId());

        return gradingResult1;
        }
    }

